package user

import (
	"fmt"
	"net/http"
	"strings"
)

func ServerMuxInit() *http.ServeMux {
	mux := http.NewServeMux()

	mux.HandleFunc("/", index)

	// Open Channel Receiver
	mux.HandleFunc("/init-open", InitOpenRespond)
	mux.HandleFunc("/open-notify", OpenNotifyRespond)
	mux.HandleFunc("/open-account", OpenAccountRespond)
	mux.HandleFunc("/open-unlock", OpenUnlockRespond)
	mux.HandleFunc("/open-commit", OpenCommitRespond)
	mux.HandleFunc("/open-create", OpenCreateRespond)

	// Update Channel Receiver
	mux.HandleFunc("/init-update", InitUpdateRespond)
	mux.HandleFunc("/update-unlock", UpdateUnlockRespond)
	mux.HandleFunc("/update-commit", UpdateCommitRespond)
	mux.HandleFunc("/update-keyExchange", UpdateKeyExchangeRespond)

	// Channel Close
	mux.HandleFunc("/init-close", InitCloseRespond)
	mux.HandleFunc("/close-create", CloseCreateRespond)

	return mux
}

func index(w http.ResponseWriter, r *http.Request) {
	_ = r.ParseForm()
	fmt.Println("Form: ", r.Form)
	fmt.Println("Path: ", r.URL.Path)
	fmt.Println(r.Form["a"])
	fmt.Println(r.Form["b"])
	for k, v := range r.Form {
		fmt.Println(k, "=>", v, strings.Join(v, "-"))
	}

	_,_ = fmt.Fprint(w, "It works !")
}
